let isAndroid = navigator.userAgent.indexOf('Android') > -1 || navigator.userAgent.indexOf('Adr') > -1;
let isiOS = !!navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);

//注册回调函数，第一次连接时调用 初始化函数(android需要初始化,ios不用)
setupWebViewJavascriptBridge(function (bridge) {
    if (isAndroid) {
        //初始化
        console.log('是否到这块')
        // bridge.init(function (message, responseCallback) {
        //     console.log(message, '初始化')
        //     var data = {
        //         'Javascript Responds': 'Wee!'
        //     };
        //     responseCallback(data);
        // })
    }
})


//这是必须要写的，用来创建一些设置
function setupWebViewJavascriptBridge(callback) {
    //Android使用
    if (isAndroid) {
		if (window.WebViewJavascriptBridge) {
		    return callback(WebViewJavascriptBridge);
		}
		if (window.WVJBCallbacks) {
		    return window.WVJBCallbacks.push(callback);
		}
		window.WVJBCallbacks = [callback];
		var WVJBIframe = document.createElement('iframe');
		WVJBIframe.style.display = 'none';
		WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__';
		document.documentElement.appendChild(WVJBIframe);
		setTimeout(function () {
		    document.documentElement.removeChild(WVJBIframe)
		}, 0);
		console.log('tag', '安卓')
		sessionStorage.phoneType = 'android'
        // if (window.WebViewJavascriptBridge) {
        //     callback(WebViewJavascriptBridge)
        // } else {
        //     document.addEventListener(
        //         'WebViewJavascriptBridgeReady',
        //         function () {
        //             callback(WebViewJavascriptBridge)
        //         },
        //         false
        //     );
        // }
        // console.log('tag', '安卓')
        // sessionStorage.phoneType = 'android'
    }

    //iOS使用
    if (isiOS) {
        if (window.WebViewJavascriptBridge) {
            return callback(WebViewJavascriptBridge);
        }
        if (window.WVJBCallbacks) {
            return window.WVJBCallbacks.push(callback);
        }
        window.WVJBCallbacks = [callback];
        var WVJBIframe = document.createElement('iframe');
        WVJBIframe.style.display = 'none';
        WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__';
        document.documentElement.appendChild(WVJBIframe);
        setTimeout(function () {
            document.documentElement.removeChild(WVJBIframe)
        }, 0);
        console.log('tag', 'ios');
        sessionStorage.phoneType = 'ios'
    }
}


export default {
    // js调APP方法 （参数分别为:app提供的方法名  传给app的数据  回调）
    callHandler(name, data, callback) {
        console.log("callHandler",name)
        setupWebViewJavascriptBridge(function (bridge) {
			console.log("callHandler",name)
            bridge.callHandler(name, data, callback)
        })
    },
    // APP调js方法 （参数分别为:js提供的方法名  回调）
      registerHandler(name, callback) {
        // #ifdef H5
        setupWebViewJavascriptBridge((bridge) => {
          bridge.registerHandler(name, (data, responseCallback) => {
            callback(data, responseCallback);
          });
        });
        // #endif
      },
}